import { ModelObjectTypeInfo, ModelObjType } from '../shared';
import { CommonResponse, IRequestHttpClient, RequestHttpClient } from './http';
// import { type HttpClient } from '@angular/common/http';

type HttpClient = any;

// const BaseUrl = '';
const BaseUrl = 'http://172.16.0.242:31200';

export class ObjModelService {
  private http: IRequestHttpClient;
  constructor(httpClient?: HttpClient) {
    let promisifyHttp;
    if (httpClient) {
      promisifyHttp = {
        get<T>(url: string): Promise<CommonResponse<T>> {
          return new Promise((resolve, reject) => {
            httpClient.get(url).subscribe({
              next: (response: any) => resolve(response),
              error: (error: any) => reject(error),
            });
          });
        },
        post<T>(url: string, body: any): Promise<CommonResponse<T>> {
          return new Promise((resolve, reject) => {
            httpClient.post(url, body).subscribe({
              next: (response: any) => resolve(response),
              error: (error: any) => reject(error),
            });
          });
        },
      };
    }
    this.http = promisifyHttp || new RequestHttpClient();
  }
  /**
   * 对象类型列表
   * detailObj 图层里的信息
   * icon gis图里的图标
   */
  async getModelObjInfo() {
    const url = BaseUrl + '/digital-service/modelObjInfo/modelObjInfo';
    const res = await this.http.get<ModelObjectTypeInfo[]>(url);
    if (res.code === 200) {
      try {
        res.data = res.data.map((item) => {
          return {
            ...item,
            detailObj: JSON.parse((item.detailObj as any as string) || '{}'),
            icon: JSON.parse((item.icon as any as string) || '{}'),
          };
        });
      } catch (err) {
        console.error('Error parsing model object info:', err);
      }
    }
    return res;
  }

  /**
   * 获取模型id
   * 需要modelType, 但是拦截器会自动添加， 所以这里不需要传递
   */
  getModelId() {
    const url = BaseUrl + '/digital-service/common/queryModelIdAndWorkingId';
    return this.http.get<{ modelIds: string; workingIdes: string }>(url);
  }

  /**
   * 获取模型GIS信息
   * @param modelObjType 模型对象类型
   * @returns
   */
  getModelGisInfo(modelObjType: ModelObjType) {
    const url =
      BaseUrl + `/digital-service/common/model/v2/json/${modelObjType}`;
    return this.http.get(url);
  }

  /**
   * 获取gis配置的最新时间
   */
  getLatestGisSettingUpdateTime() {
    const url =
      BaseUrl + '/digital-service/gis/label/group/params/release/time';
    return this.http.get<{ createTime: string }>(url);
  }
}
